package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result page(EmpQueryParam param) {
        log.info("查询员工,员工信息:{}",param);
        //开始分页
        Page<Emp> page = new Page<>(param.getPage(),param.getPageSize());

        page.addOrder(OrderItem.desc("update_time"));//根据更新时间降序排序
        //条件查询
        LambdaQueryWrapper<Emp> lqw = new LambdaQueryWrapper<>();
        //根据性别,入职日期,名称模糊查询
        lqw.eq(param.getGender() != null,Emp::getGender,param.getGender());
        lqw.between(param.getBegin() != null && param.getEnd() != null,Emp::getEntryDate,param.getBegin(),param.getEnd());
        lqw.like(param.getName() != null,Emp::getName,param.getName());

        Page<Emp> page1 = empService.page(page, lqw);

        //获取分页数据
        PageResult<Emp> pageResult = new PageResult<>(page1.getTotal(),page1.getRecords());
        //获取员工的部门id
        List<Emp> emps = page.getRecords();
        Set<Integer> idset = emps.stream().map(Emp::getDeptId).filter(Objects::nonNull).collect(Collectors.toSet());
        if (idset != null && idset.size()>0){
            //获取部门id
            List<Dept> depts = deptService.listByIds(idset);
            //给员工的部门根据id加上部门的名称
            for (Dept dept : depts) {
                for (Emp emp : emps) {
                    //非空判断
                    if (emp.getId() != null && emp.getDeptId()!= null && emp.getDeptId().equals(dept.getId())){
                        emp.setDeptName(dept.getName());
                    }
                }
            }
        }

        return Result.success(pageResult);
    }

    @PostMapping
    public Result save(@RequestBody Emp emp) {
        log.info("新增员工，员工信息：{}", emp);

        // 调用service层方法保存员工信息和员工的工作经历信息
        empService.saveEmp(emp);

        return Result.success();
    }

    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("批量删除学生，{}", ids);

        // 调用service层方法批量删除员工信息和员工工作经历信息
        empService.deleteByIds(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询员工信息，id：{}", id);

        Emp empById = empService.getEmpById(id);
        return Result.success(empById);
    }

    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改员工信息，员工信息：{}", emp);

        // 调用service层方法修改员工信息

        empService.updateEmp(emp);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        log.info("查询员工列表");

        // 调用service层方法查询员工列表
        return Result.success();
    }
}
